﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tools;
using Server.Models;
using DataAccess;

namespace Server.Controllers
{
    public class ImgController : Controller
    {
        //
        // GET: /Img/
        /// <summary>
        /// 根据站点编号得到最新图片
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        public ActionResult GetImg(string mn)
        {
            string MnImgPath = System.Configuration.ConfigurationManager.AppSettings["MnImg"];
            string Replace = System.Configuration.ConfigurationManager.AppSettings["Replace"];
            string sql = @"select 
            top 1
	        a.monitoring_point_name as MnName,
	        a.monitoring_point_mn as Mn,
	        b.STT as datatime,
	        b.PIC as ImgPath
         from monitoring_points_info as a inner join PicData as b on a.monitoring_point_mn=b.ST where b.ST=@mn order by
         b.STT desc;
                ";
            SqlParameter sp_mn = new SqlParameter("@mn", mn);
            DataTable dt = SqlHelper.Query(SqlHelper.NFW, CommandType.Text, sql, sp_mn).Tables[0];
            List<ImgModel> list = DataTableToList<ImgModel>.ConvertToModel(dt);
            foreach (var item in list)
            {
                item.ImgPath = MnImgPath + item.ImgPath.Replace(Replace, "").Replace("\\", "/");
            }
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented, timeFormat);

            return Content(json, "application/json");

        }

    }

}
